package com.heavenbird.book.dao.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.heavenbird.book.BookConst;
import com.heavenbird.book.dao.interfaces.IUserAccountDAO;
import com.heavenbird.book.entity.UserAccount;
import com.heavenbird.common.util.DBUtil;

public class UserAccountDAOImpl implements IUserAccountDAO {
	private static transient Log log = LogFactory.getLog(UserAccountDAOImpl.class);
	
	public void delUserAccount(UserAccount userAccount) throws Exception {
		Session session = DBUtil.getSession();
		session.delete(userAccount);
	}
	
	public void updateUserAccount(UserAccount userAccount) throws Exception {
		Session session = DBUtil.getSession();
		session.update(userAccount);
	}
	
	public void newUserAccount(UserAccount userAccount) throws Exception {
		Session session = DBUtil.getSession();
		session.save(userAccount);
	}
	
	public UserAccount[] queryUserAccount(long userId, int sts) throws Exception {
		Session session = DBUtil.getSession();
		Criteria criteria = session.createCriteria(UserAccount.class);
		criteria.add(Restrictions.eq("userId", userId));
		if (sts != BookConst.USERACCOUNT.STS_0) {
			criteria.add(Restrictions.eq("sts", sts));
		}
		criteria.addOrder(Order.asc("sortId"));
		List list = criteria.list();
		return (UserAccount[])list.toArray(new UserAccount[list.size()]);
	}
	
	public UserAccount[] getUserAccount(long userId, long acctId, int sts) throws Exception {
		Session session = DBUtil.getSession();
		Criteria criteria = session.createCriteria(UserAccount.class);
		criteria.add(Restrictions.eq("userId", userId));
		criteria.add(Restrictions.eq("acctId", acctId));
		if (sts != BookConst.USERACCOUNT.STS_0) {
			criteria.add(Restrictions.eq("sts", sts));
		}
		criteria.addOrder(Order.asc("sortId"));
		List list = criteria.list();
		return (UserAccount[])list.toArray(new UserAccount[list.size()]);
	}
	
}
